class CommentsController < ApplicationController

	before_action :set_product, only: [:show, :destroy]

  def index
  	@products = Product.all

  end

  def new
  	# @comment = Product.find(params[:product_id]).comments.new
  	@product = Product.find(params[:product_id])
  	@comment = Comment.new
  end

  def create
  	@comment = Product.find(params[:product_id]).comments.new(product_params)

    if @comment.save
      redirect_to products_path, notice: "Comment successfully added!"
    else
      render action: "new"
    end
  end

  def destroy
  	@comment.destroy
    redirect_to products_url
  end

  private

  def product_params
    params.require(:comment).permit(:author, :content, :product_id)
  end

  def set_product
    @comment = Comment.find(params[:id])
  end

end
